#json !/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import sys
import datetime
reload(sys) 
sys.setdefaultencoding('utf8')
import time
import MySQLdb
sys.path.insert(0,'/home/liuyue/svn/wan/')
sys.path.insert(0,'/opt/web/')
sys.path.insert(0,'/opt/wanalytics/')
from Tools import connectMySQL
from datetime import date
from datetime import timedelta

def Total():
    
    
    
    
    _yesday = datetime.datetime.today() + datetime.timedelta(-1)
    _datetime = _yesday.strftime('%Y%m%d')
    
    
    #_datesql = " "
    #数据库连接
    try:
        db = connectMySQL.connMySQL("stat")
    except MySQLdb.Error, e:
        resultStr = '{"status": "failed", "code": "4001", "str": "connect database failed"}'
        return resultStr
    db.autocommit(True)
    c = db.cursor()
    
    #sql = " select date from stat_vid_board group by date "
    #c.execute(sql)
    #days_l = c.fetchall()
    
    days_l = [[_datetime]]
    
    #print days_l
    #sys.exit()
    
    os_array = ['61,59,52','61','59','52']
    device_array = ['1,2','1','2']
    
    for days_v in days_l:
        for os in os_array:
            for device in device_array:
                
                riqis = str(days_v[0])
                
                yy = riqis[0:4]
                mm = riqis[4:6]
                dd = riqis[6:]
                
                start = date(year=int(yy),month=int(mm),day=int(dd))
                start_day = start - timedelta(days=start.weekday()) + timedelta(days=0, weeks=-1)
                end_day = start - timedelta(days=start.weekday()) + timedelta(days=6, weeks=-1)
                start_day = start_day.strftime('%Y%m%d')
                end_day = end_day.strftime('%Y%m%d')
                

             
                sql = " SELECT sum(playtimes),vid,os_id,device_type FROM `stat_vid_board` where os_id in (%s) and device_type in (%s) and date >= %s and date <= %s group by vid " % (os,device,start_day,end_day)
                #print sql
                c.execute(sql)  
                result = c.fetchall()
                
                for x in result:
                    sql = " insert into `stat_vid_board_week` (`playtimes`,`vid`,`os_id`,`device_type`,`start_date`,`end_date`) values ('%s','%s','%s','%s','%s','%s') " % (x[0],x[1],x[2],x[3],start_day,end_day)
                    #print sql
                    try:
                        c.execute(sql)
                    except Exception,e:
                        c.execute("update stat_vid_board_week set playtimes = '%s' where vid = '%s' and os_id = '%s' and device_type = %s and start_date = '%s' and end_date = '%s' " % (x[0],x[1],x[2],x[3],start_day,end_day) )
                        #print e
                
      
    
    c.close()
    db.close()
    
    
    





if __name__ == '__main__':
    cmd = ''
    if len(sys.argv) == 2:
        cmd = sys.argv[1]

    if cmd == 'total':
        print "Total()"
        Total()
        print "Done !"
    else:
        print "Nothing Happen!"
